﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
public partial class NhanSu : System.Web.UI.Page
{
    clsXuLy ex = new clsXuLy();
    DataTable dt;
    SqlParameter[] p;
    string frmName = "frmNhanSu";
    static int STT = 0;
    static ReportDocument rpt;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dtChiTiet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Title = "Thông tin nhân sự | " + Header;
            //Load CV
            LoadDrop(dlChucVu, "sp_vnttblchucvu_List", "--Chọn chức vụ--");
            //Load PB
            LoadDrop(dlPhongBan, "sp_vnttblphongban_List", "--Chọn phòng ban--");
            btnCancel_Click(sender, e);
            LoadDanhSach();
            btncapnhat.Visible = false;

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       // try
        //{
            string date = DateTime.Now.ToString("ddMMyyhhmmss");
            p = new SqlParameter[30];
            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@MaNV", txtMaNV.Text.Trim());
            p[1].SqlDbType = SqlDbType.VarChar;
            p[2] = new SqlParameter("@HoTen", txtHoTen.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;
            p[3] = new SqlParameter("@MaHS", txtMaHS.Text.Trim());
            p[3].SqlDbType = SqlDbType.VarChar;
            p[4] = new SqlParameter("@ChucVu", ex.GetGuid(dlChucVu.SelectedValue));
            p[4].SqlDbType = SqlDbType.UniqueIdentifier;
            p[5] = new SqlParameter("@PhongBan", ex.GetGuid(dlPhongBan.SelectedValue));
            p[5].SqlDbType = SqlDbType.UniqueIdentifier;
            p[6] = new SqlParameter("@NgaySinh", ex.GetNgayThang(txtNgaySinh.Text.Trim()));
            p[6].SqlDbType = SqlDbType.SmallDateTime;
            p[7] = new SqlParameter("@DienThoai", txtDienThoai.Text.Trim());
            p[7].SqlDbType = SqlDbType.VarChar;
            if(ck_Nam.Checked==true)
                p[8] = new SqlParameter("@GioiTinh", "Nam");
            else
                p[8] = new SqlParameter("@GioiTinh", "Nữ");
            p[8].SqlDbType = SqlDbType.NVarChar;
            p[9] = new SqlParameter("@DanToc", txtDanToc.Text.Trim());
            p[9].SqlDbType = SqlDbType.NVarChar;
            p[10] = new SqlParameter("@ChieuCao", txtChieuCao.Text.Trim());
            p[10].SqlDbType = SqlDbType.VarChar;
            p[11] = new SqlParameter("@CMND",txtcmtnd.Text.Trim());
            p[11].SqlDbType = SqlDbType.VarChar;
            p[12] = new SqlParameter("@NgayCap", txtNgayCap.Text.Trim());
            p[12].SqlDbType = SqlDbType.VarChar;
            p[13] = new SqlParameter("@MSTCaNhan", txtMSTCaNhan.Text.Trim());
            p[13].SqlDbType = SqlDbType.NVarChar;
            if (ck_KetHon.Checked == true)
                p[14] = new SqlParameter("@TinhTrangHonNhan", "1");
            else
                p[14] = new SqlParameter("@TinhTrangHonNhan", "0");
            p[14].SqlDbType = SqlDbType.NVarChar;
            p[15] = new SqlParameter("@QueQuan", txtQueQuan.Text.Trim());
            p[15].SqlDbType = SqlDbType.NVarChar;
            p[16] = new SqlParameter("@NoiOHienNay",txtNoiohientai.Text.Trim());
            p[16].SqlDbType = SqlDbType.NVarChar;
            p[17] = new SqlParameter("@TrinhDoHV",txtTrinhDo.Text.Trim());
            p[17].SqlDbType = SqlDbType.NVarChar;
            p[18] = new SqlParameter("@ChuyenMon", txtChuyenMon.Text.Trim());
            p[18].SqlDbType = SqlDbType.NVarChar;
            p[19] = new SqlParameter("@BangCap", txtBangCap.Text.Trim());
            p[19].SqlDbType = SqlDbType.NVarChar;
            p[20] = new SqlParameter("@NgayThuViec", txtNgaythuviec.Text.Trim());
            p[20].SqlDbType = SqlDbType.NVarChar;
            p[21] = new SqlParameter("@ThoiGianThuViec", txtThoigianthuviec.Text.Trim());
            p[21].SqlDbType = SqlDbType.NVarChar;
            p[22] = new SqlParameter("@NgayChinhThuc", txtNgaychinhthuc.Text.Trim());
            p[22].SqlDbType = SqlDbType.NVarChar;
            p[23] = new SqlParameter("@ThoiGianHopDong",txtThoigianhopdong.Text.Trim());
            p[23].SqlDbType = SqlDbType.NVarChar;
            p[24] = new SqlParameter("@NgayhetHanHD", ex.GetNgayThang(txtNgayhethanhopdong.Text.Trim()));
            p[24].SqlDbType = SqlDbType.NVarChar;
            p[25] = new SqlParameter("@Email", txtEmail.Text.Trim());
            p[25].SqlDbType = SqlDbType.VarChar;
            p[26] = new SqlParameter("@EmailCongty",txtEmailcongty.Text.Trim());
            p[26].SqlDbType = SqlDbType.VarChar;
            if(fu.HasFile)
                p[27] = new SqlParameter("@HinhAnh", "NhanSu/Image/" + date + fu.FileName.Substring(fu.FileName.LastIndexOf('.')));
            else
                p[27] = new SqlParameter("@HinhAnh", DBNull.Value);
            p[27].SqlDbType = SqlDbType.NVarChar;
            p[28] = new SqlParameter("@NoiCap",txtNoiCap.Text.Trim());
            p[28].SqlDbType = SqlDbType.NVarChar;
            p[29] = new SqlParameter("@DiaChiThuongTru",txtNoiohientai.Text.Trim());
            p[29].SqlDbType = SqlDbType.NVarChar;
            ex.ExecuteNonQuerry("sp_vnttblnhansu_Add", p);
            if (fu.HasFile)
                fu.SaveAs(Server.MapPath("NhanSu/Image/" + date + fu.FileName.Substring(fu.FileName.LastIndexOf('.'))));

            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");
            LoadDanhSach();
        //}
       //catch (Exception ax)
       // {
        //    gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
          //  ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhân sự\t" + ax.Message);
        //}
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        btncapnhat.Visible = false;
        btnSave.Visible = true;
        txtMaNV.Text = "";
        txtMaNV.Focus();
        txtHoTen.Text = "";
        txtMaHS.Text = "";
        dlChucVu.SelectedIndex = 0;
        dlPhongBan.SelectedIndex = 0;
        txtNgaySinh.Text = "";
        txtDienThoai.Text = "";
        txtDanToc.Text = "";
        ck_Nam.Checked = true;
        txtChieuCao.Text = "";
        txtcmtnd.Text ="";
        txtNgayCap.Text = "";
        txtNoiCap.Text = "";
        txtMSTCaNhan.Text ="";
        ck_ChuaKetHon.Checked = true;
        txtQueQuan.Text = "";
        txtNoiohientai.Text = "";
        txtTrinhDo.Text = "";
        txtChuyenMon.Text ="";
        txtBangCap.Text = "";
        txtNgaythuviec.Text = "";
        txtThoigianthuviec.Text = "";
        txtNgaychinhthuc.Text = "";
        txtThoigianhopdong.Text = "";
        txtNgayhethanhopdong.Text ="";
        txtEmail.Text = "";
        txtEmailcongty.Text = "";

    }
    private void LoadDanhSach()
    {
 
        if (string.IsNullOrEmpty(Convert.ToString(Session["IDUser"])) || string.IsNullOrEmpty(Convert.ToString(Session["NhomND"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            dt = ex.GetData("sp_vnttblnhansu_ListByAdmin");
            GV.DataSource = dt;
            GV.DataBind();
        }

    }
    protected void btnPDF_Click(object sender, EventArgs e)
    {
        //if (hdID.Value.Trim().Equals(""))
        //    gstGetMess("Hết phiên làm việc, vui lòng chọn lại văn bản để xuất", "");
       // else
       // {
            Guid ID = new Guid(hdID.Value);
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@ID", ID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;

            dt = ex.GetData("sp_vnttblnhansu_Print", p);
           if (dt.Rows.Count > 0)
               ViewReport(dt.Rows[0]["MaNV"].ToString().Trim(), dt.Rows[0]["HinhAnh"].ToString().Trim(), dt.Rows[0]["HoTen"].ToString(), dt.Rows[0]["MaHS"].ToString(), dt.Rows[0]["TenCV"].ToString(), dt.Rows[0]["TenPB"].ToString(), dt.Rows[0]["NgaySinh"].ToString(), dt.Rows[0]["DienThoai"].ToString(), dt.Rows[0]["Email"].ToString());
       // }
    }
    void ViewReport(string MaNV, string HinhAnh, string HoTen, string MaHS, string TenCV, string TenPB, string NgaySinh, string DienThoai, string Email)
    {
        string path = Server.MapPath("Report/BC.PDF");
        rpt = new ReportDocument();
        rpt.Load(Server.MapPath("Report/NhanSu.rpt"));

        rpt.SetParameterValue("@MaNV", MaNV);
        rpt.SetParameterValue("@HinhAnh", HinhAnh);
        rpt.SetParameterValue("@HoTen", HoTen);
        rpt.SetParameterValue("@MaHS", MaHS);
        rpt.SetParameterValue("@TenCV", TenCV);
        rpt.SetParameterValue("@TenPB", TenPB);
        rpt.SetParameterValue("@NgaySinh", NgaySinh);
        rpt.SetParameterValue("@DienThoai", DienThoai);
        rpt.SetParameterValue("@Email", Email);
        
        rpt.ExportToDisk(ExportFormatType.PortableDocFormat, path);
        HttpResponse res = HttpContext.Current.Response;
        res.ClearContent();
        res.ContentType = "application/pdf";
        res.AddHeader("Content-Disposition", "Attachment; filename=VNT.NS.07" + ".pdf" + ";");

        MemoryStream stream;
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] img = new byte[fs.Length];
        fs.Read(img, 0, Convert.ToInt32(fs.Length));
        fs.Close();
        fs.Dispose();
        stream = new MemoryStream(img);
        stream.WriteTo(Response.OutputStream);

        FileInfo f = new FileInfo(path);
        if (f.Exists) f.Delete();
        res.End();
    }
    private void LoadDrop(DropDownList dl,string proc,string tieude)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData(proc);
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(tieude,""));
        dl.SelectedIndex=0;
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        try
        {
            Guid gID = new Guid(hdID.Value);
            string date = DateTime.Now.ToString("ddMMyyhhmmss");
            p = new SqlParameter[30];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@MaNV", txtMaNV.Text.Trim());
            p[1].SqlDbType = SqlDbType.VarChar;
            p[2] = new SqlParameter("@HoTen", txtHoTen.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;
            p[3] = new SqlParameter("@MaHS", txtMaHS.Text.Trim());
            p[3].SqlDbType = SqlDbType.VarChar;
            p[4] = new SqlParameter("@ChucVu", ex.GetGuid(dlChucVu.SelectedValue));
            p[4].SqlDbType = SqlDbType.UniqueIdentifier;
            p[5] = new SqlParameter("@PhongBan", ex.GetGuid(dlPhongBan.SelectedValue));
            p[5].SqlDbType = SqlDbType.UniqueIdentifier;
            p[6] = new SqlParameter("@NgaySinh", ex.GetNgayThang(txtNgaySinh.Text.Trim()));
            p[6].SqlDbType = SqlDbType.SmallDateTime;
            p[7] = new SqlParameter("@DienThoai", txtDienThoai.Text.Trim());
            p[7].SqlDbType = SqlDbType.VarChar;
            if (ck_Nam.Checked == true)
                p[8] = new SqlParameter("@GioiTinh", "Nam");
            else
                p[8] = new SqlParameter("@GioiTinh", "Nữ");
            p[8].SqlDbType = SqlDbType.NVarChar;
            p[9] = new SqlParameter("@DanToc", txtDanToc.Text.Trim());
            p[9].SqlDbType = SqlDbType.NVarChar;
            p[10] = new SqlParameter("@ChieuCao", txtChieuCao.Text.Trim());
            p[10].SqlDbType = SqlDbType.VarChar;
            p[11] = new SqlParameter("@CMND", txtcmtnd.Text.Trim());
            p[11].SqlDbType = SqlDbType.VarChar;
            p[12] = new SqlParameter("@NgayCap", txtNgayCap.Text.Trim());
            p[12].SqlDbType = SqlDbType.VarChar;
            p[13] = new SqlParameter("@MSTCaNhan", txtMSTCaNhan.Text.Trim());
            p[13].SqlDbType = SqlDbType.NVarChar;
            if (ck_KetHon.Checked == true)
                p[14] = new SqlParameter("@TinhTrangHonNhan", "1");
            else
                p[14] = new SqlParameter("@TinhTrangHonNhan", "0");
            p[14].SqlDbType = SqlDbType.NVarChar;
            p[15] = new SqlParameter("@QueQuan", txtQueQuan.Text.Trim());
            p[15].SqlDbType = SqlDbType.NVarChar;
            p[16] = new SqlParameter("@NoiOHienNay", txtNoiohientai.Text.Trim());
            p[16].SqlDbType = SqlDbType.NVarChar;
            p[17] = new SqlParameter("@TrinhDoHV", txtTrinhDo.Text.Trim());
            p[17].SqlDbType = SqlDbType.NVarChar;
            p[18] = new SqlParameter("@ChuyenMon", txtChuyenMon.Text.Trim());
            p[18].SqlDbType = SqlDbType.NVarChar;
            p[19] = new SqlParameter("@BangCap", txtBangCap.Text.Trim());
            p[19].SqlDbType = SqlDbType.NVarChar;
            p[20] = new SqlParameter("@NgayThuViec", txtNgaythuviec.Text.Trim());
            p[20].SqlDbType = SqlDbType.NVarChar;
            p[21] = new SqlParameter("@ThoiGianThuViec", txtThoigianthuviec.Text.Trim());
            p[21].SqlDbType = SqlDbType.NVarChar;
            p[22] = new SqlParameter("@NgayChinhThuc", ex.GetNgayThang(txtNgaychinhthuc.Text.Trim()));
            p[22].SqlDbType = SqlDbType.NVarChar;
            p[23] = new SqlParameter("@ThoiGianHopDong", txtThoigianhopdong.Text.Trim());
            p[23].SqlDbType = SqlDbType.NVarChar;
            p[24] = new SqlParameter("@NgayhetHanHD", txtNgayhethanhopdong.Text.Trim());
            p[24].SqlDbType = SqlDbType.NVarChar;
            p[25] = new SqlParameter("@Email", txtEmail.Text.Trim());
            p[25].SqlDbType = SqlDbType.VarChar;
            p[26] = new SqlParameter("@EmailCongty", txtEmailcongty.Text.Trim());
            p[26].SqlDbType = SqlDbType.VarChar;
            if (fu.HasFile)
                p[27] = new SqlParameter("@HinhAnh", "NhanSu/Image/" + date + fu.FileName.Substring(fu.FileName.LastIndexOf('.')));
            else
                p[27] = new SqlParameter("@HinhAnh", DBNull.Value);
            p[27].SqlDbType = SqlDbType.NVarChar;
            p[28] = new SqlParameter("@NoiCap", txtNoiCap.Text.Trim());
            p[28].SqlDbType = SqlDbType.NVarChar;
            p[29] = new SqlParameter("@DiaChiThuongTru", txtNoiohientai.Text.Trim());
            p[29].SqlDbType = SqlDbType.NVarChar;
            ex.ExecuteNonQuerry("sp_vnttblnhansu_Update", p);
            if (fu.HasFile)
                fu.SaveAs(Server.MapPath("NhanSu/Image/" + date + fu.FileName.Substring(fu.FileName.LastIndexOf('.'))));
            btnCancel_Click(sender, e);
            gstGetMess("Cập nhật thành công", "");
            LoadDanhSach();
        }
        catch (Exception ax)
        {

            gstGetMess("Lỗi: " + ax.Message, "");
        }
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        btncapnhat.Visible = true;
        btnSave.Visible = false;
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            dtChiTiet = ex.GetData("sp_vnttblnhansu_ListByID", p);
            if (dtChiTiet.Rows.Count > 0)
            {
                try
                {
                    txtMaNV.Text = dtChiTiet.Rows[0]["MaNV"].ToString();
                    txtHoTen.Text = dtChiTiet.Rows[0]["HoTen"].ToString();
                    txtMaHS.Text = dtChiTiet.Rows[0]["MaNV"].ToString();
                    dlChucVu.SelectedValue = dtChiTiet.Rows[0]["ChucVu"].ToString();
                    dlPhongBan.SelectedValue = dtChiTiet.Rows[0]["PhongBan"].ToString();
                    txtNgaySinh.Text = dtChiTiet.Rows[0]["NgaySinh2"].ToString();
                    txtDienThoai.Text = dtChiTiet.Rows[0]["DienThoai"].ToString();
                    
                    if (dtChiTiet.Rows[0]["GioiTinh"].ToString() == "Nam")
                        ck_Nam.Checked = true;
                    else
                        ck_Nu.Checked = true;
                    txtDanToc.Text = dtChiTiet.Rows[0]["DanToc"].ToString();
                    txtChieuCao.Text = dtChiTiet.Rows[0]["ChieuCao"].ToString();
                    txtcmtnd.Text = dtChiTiet.Rows[0]["CMND"].ToString();
                    txtNgayCap.Text = dtChiTiet.Rows[0]["NgayCap"].ToString();
                    txtNoiCap.Text = dtChiTiet.Rows[0]["NoiCap"].ToString();
                    txtMSTCaNhan.Text = dtChiTiet.Rows[0]["MSTCaNhan"].ToString();
                    if (dtChiTiet.Rows[0]["TinhTrangHonNhan"].ToString() == "0")
                        ck_ChuaKetHon.Checked = true;
                    else
                        ck_KetHon.Checked = true;
                    txtQueQuan.Text = dtChiTiet.Rows[0]["QueQuan"].ToString();
                    txtNoiohientai.Text = dtChiTiet.Rows[0]["NoiOHienNay"].ToString();
                    txtTrinhDo.Text = dtChiTiet.Rows[0]["TrinhDoHV"].ToString();
                    txtChuyenMon.Text = dtChiTiet.Rows[0]["ChuyenMon"].ToString();
                    txtBangCap.Text = dtChiTiet.Rows[0]["BangCap"].ToString();
                    txtNgaythuviec.Text = dtChiTiet.Rows[0]["NgayThuViec"].ToString();
                    txtThoigianthuviec.Text = dtChiTiet.Rows[0]["ThoiGianThuViec"].ToString();
                    txtNgaychinhthuc.Text = dtChiTiet.Rows[0]["NgayChinhThuc"].ToString();
                    txtThoigianhopdong.Text= dtChiTiet.Rows[0]["ThoiGianHopDong"].ToString();
                    txtNgayhethanhopdong.Text = dtChiTiet.Rows[0]["NgayHetHanHD"].ToString();
                    txtEmail.Text = dtChiTiet.Rows[0]["Email"].ToString();
                    txtEmailcongty.Text= dtChiTiet.Rows[0]["EmailCongTy"].ToString();
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhân sự\t" + ax.Message);
                }
            }
            btnSave.Visible = false;
            btncapnhat.Visible = true;

        }
        else
            if (e.CommandName.Equals("Del"))
            {

                try
                {
                    ex.ExecuteNonQuerry("sp_vnttblnhansu_Del", p);
                    btnCancel_Click(sender, e);
                    gstGetMess("Xóa thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Lỗi: " + ax.Message, "");

                }

            }
        //LoadDanhSach();
    }
    
}